<template>
    <div>
        <div class="add-btn">
            <el-button type="primary" @click="handleOpen">增加</el-button>
        </div>

        <el-dialog
            v-model="dialogFormVisible"
            :destroy-on-close="true"
            :close-on-click-modal="false"
            title="增加图文内容"
            @close="handleClose"
        >
            <el-form :model="content">
                <el-form-item label="标题" :label-width="formLabelWidth">
                    <el-input v-model="content.title" autocomplete="off" />
                </el-form-item>
                <el-form-item label="内容" :label-width="formLabelWidth">
                    <el-input
                        v-model="content.text"
                        :rows="4"
                        type="textarea"
                        autocomplete="off"
                    />
                </el-form-item>
                <el-form-item label="选择类别" :label-width="formLabelWidth">
                    <el-select
                        v-model="content.typeId"
                        class="m-2"
                        placeholder="请选择类别"
                        size="large"
                    >
                        <el-option
                            v-for="item in types"
                            :key="item.id"
                            :label="item.name"
                            :value="item.id"
                        />
                    </el-select>
                </el-form-item>
                <el-form-item label="上传" :label-width="formLabelWidth">
                    <Upload></Upload>
                </el-form-item>
            </el-form>
            <template #footer>
                <span class="dialog-footer">
                    <el-button @click="dialogFormVisible = false"
                        >取消</el-button
                    >
                    <el-button type="primary" @click="handleAdd">
                        确定
                    </el-button>
                </span>
            </template>
        </el-dialog>
    </div>
</template>
<script setup>
import Upload from "@/components/Upload.vue";

import { ElMessage  } from "element-plus";
import { defineEmits, ref } from "vue";
import { addContent } from "@/services/content";
import { useContentStore } from "@/stores/content";

import { storeToRefs } from "pinia";
// import { deleteFile } from "@/services";
const contentStore = useContentStore();
const { fetchContents,fetchDeleteFiles } = contentStore;
const { fileList,removeFileList } = storeToRefs(contentStore);
// const emit = defineEmits(['show']);
const props = defineProps(['types']);
const dialogFormVisible = ref(false);
const content = ref({ title: "", text: "" });
const formLabelWidth = ref(100);
// const allTpyes = ref([]);
const handleOpen = async () => {
    dialogFormVisible.value = true;
};
const handleClose = () => {
    
    content.value = { title: "", text: "" };
    removeFileList.value = [...removeFileList.value,...fileList.value.map(item => item.url)];
    fileList.value = [];
    fetchDeleteFiles();
};
const handleAdd = async () => {
    if(!content.value.typeId){
        ElMessage.error('类别不能为空');
        return 
    }
    
    // const images = fileList.value.filter((item) => item.url).map((item) => ({
    //     url: item.url,
    // }));
    const images = fileList.value.filter(item => item.url).map(item => item.url);
    if(images.length == 0){
        ElMessage.error('图片不能为空');
        return 
    }
    fileList.value = [];
    // content.value.images = JSON.stringify(images);
    content.value.images = images;
    await addContent(content.value);
    // emit('show');
    console.log("content:",content.value);
    await fetchContents({}, {});
    dialogFormVisible.value = false;
};
</script>
<style>
.add-btn {
    display: flex;
    justify-content: flex-end;
}
</style>
